package com.estate.web.controller.eastate;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.estate.common.annotation.Log;
import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.page.TableDataInfo;
import com.estate.common.enums.BusinessType;
import com.estate.common.utils.poi.ExcelUtil;
import com.estate.system.domain.Carousel;
import com.estate.system.domain.CarouselDetail;
import com.estate.system.service.ICarouselDetailService;
import com.estate.system.service.ICarouselService;
import com.estate.system.service.ISysAdvantageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author estate
 * @date 2022-04-08
 */
@Controller
@RequestMapping("/system/carousel")
@CrossOrigin

public class CarouselController extends BaseController
{
    private String prefix = "system/carousel";

    @Autowired
    private ICarouselService carouselService;
    @Autowired
    private ICarouselDetailService carouselDetailService;


    @RequiresPermissions("system:carousel:view")
    @GetMapping()
    public String carousel()
    {
        return prefix + "/carousel";
    }



    /**
     * 查询【请填写功能名称】列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Carousel carousel)
    {
        startPage();
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        return getDataTable(list);
    }
    @PostMapping("/list/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Carousel> listByPageHelper(Carousel carousel, @PathVariable int pageSize, @PathVariable int pageNum)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        PageInfo<Carousel> pageInfo = new PageInfo<Carousel>(list);

        return pageInfo;
    }

    /**
     * 得到上传的图片地址并返回标签,1张图片
     */

    @PostMapping("/selectCarouselImg")
    @ResponseBody
    public String afterUploadFileUpdateData(int carouselId)
    {
        Carousel carousel = carouselService.selectCarouselById((long) carouselId);
        return carousel.getImgAddress();
    }



    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:carousel:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Carousel carousel)
    {
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        ExcelUtil<Carousel> util = new ExcelUtil<Carousel>(Carousel.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {

        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:carousel:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Carousel carousel)
    {
        carousel.setImgAddress("[]");
        int res=carouselService.insertCarousel(carousel);

        if (res ==1){
            return toAjax(true).put("id",carousel.getId());
        }
        return toAjax(false).put("id",0).put("msg","插入失败或插入数据大于4条");
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:carousel:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        System.out.println("into");
        Carousel carousel = carouselService.selectCarouselById(id);
        CarouselDetail carouselDetail = carouselDetailService.selectCarouselDetailById(id);
        mmap.put("carousel", carousel);


        mmap.put("id",id);

        return prefix + "/edit";
    }
//    @RequiresPermissions("system:carousel:edit")
    @GetMapping("/edit_detail/{id}")
    public String edit_detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        //根据id搜索,detail表的数据,不存在则返回null
        Carousel carousel = carouselService.selectCarouselById(id);
        mmap.put("carousel", carousel);
        CarouselDetail carouselDetail = carouselDetailService.selectCarouselDetailById(id);
        mmap.put("carouselDetail", carouselDetail);
        return prefix + "/edit_detail";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:carousel:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Carousel carousel,ModelMap mmap)
    {

          carousel.setImgAddress("[]");

        return toAjax(carouselService.updateCarousel(carousel));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:carousel:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carouselService.deleteCarouselByIds(ids));
    }
}
